﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;

namespace Sharp.UnusedReferences
{
	class VsSolutionEvents : IVsSolutionEvents, IDisposable
	{
		#region Fields
		
		IVsSolution2 solution;
		private uint pdwCookie;

		#endregion

		#region Constructors

		public VsSolutionEvents()
		{
			solution = ServiceProvider.GlobalProvider.GetService(typeof(SVsSolution)) as IVsSolution2;
			if(solution != null)
			{
				solution.AdviseSolutionEvents(this, out pdwCookie);
			}
		}
		#endregion

		public event EventHandler AfterOpenSolution;
		public event EventHandler AfterCloseSolution;
		
		#region IVsSolutionEvents Members

		public int OnAfterCloseSolution(object pUnkReserved)
		{
			if(AfterCloseSolution != null)
			{
				AfterCloseSolution(this, EventArgs.Empty);
			}
			return VSConstants.S_OK;
		}

		public int OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy)
		{
			return VSConstants.S_OK;
		}

		public int OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded)
		{
			return VSConstants.S_OK;
		}

		public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
		{
			if(AfterOpenSolution != null)
			{
				AfterOpenSolution(this, EventArgs.Empty);
			}
			return VSConstants.S_OK;
		}

		public int OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved)
		{
			return VSConstants.S_OK;
		}

		public int OnBeforeCloseSolution(object pUnkReserved)
		{
			return VSConstants.S_OK;
		}

		public int OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy)
		{
			return VSConstants.S_OK;
		}

		public int OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel)
		{
			return VSConstants.S_OK;
		}

		public int OnQueryCloseSolution(object pUnkReserved, ref int pfCancel)
		{
			return VSConstants.S_OK;
		}

		public int OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel)
		{
			return VSConstants.S_OK;
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			if(solution != null && pdwCookie != 0)
			{
				solution.UnadviseSolutionEvents(pdwCookie);
			}
		}

		#endregion
	}
}
